package com.zhangsr.mongodb.client.sample;

import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.QueryBuilder;
import com.mongodb.WriteResult;
import com.zhangsr.mongodb.client.MongoDBClient;
import com.zhangsr.mongodb.client.constants.MongoConstants;
import com.zhangsr.mongodb.client.driver.MongoDBDriver;
import com.zhangsr.mongodb.client.util.ConverterUtil;
import net.vz.mongodb.jackson.internal.MongoAnnotationIntrospector;
import net.vz.mongodb.jackson.internal.MongoJacksonHandlerInstantiator;
import net.vz.mongodb.jackson.internal.MongoJacksonMapperModule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @Author: zhangsr
 */
public class SimpleMongoDBClient<E> implements MongoDBClient<E> {

    public static final Log logger = LogFactory.getLog(MongoConstants.COMMON_MONGO);

    private MongoDBDriver driver;
    private String collectionName;
    private boolean reconstructMongoId = true;

    private ObjectMapper objectMapper = null;
    private DBCollection collection;

    public void init() {
        try {
            objectMapper = new ObjectMapper();
            objectMapper.withModule(new MongoJacksonMapperModule());
            objectMapper.setHandlerInstantiator(new MongoJacksonHandlerInstantiator(new MongoAnnotationIntrospector(objectMapper.getDeserializationConfig())));
            collection = driver.getDatabase().getCollection(collectionName);
        } catch (Exception e) {
            logger.error("SimpleMongoDBClient 初始化失败", e);
        }
    }

    @Override
    public boolean insert(E bean) {
        DBObject dbObj = ConverterUtil.convertToDbObject(bean, objectMapper);

        WriteResult result = collection.insert(dbObj);
        if (result.getError() != null) {
            logger.error("MongoDB 插入异常:" + result.getError());
            return false;
        }
        return true;
    }

    @Override
    public boolean save(QueryBuilder condition, E bean) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean delete(E bean) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean delete(QueryBuilder condition) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public E selectOne(QueryBuilder condition, Class<E> beanClass) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    //---------------------- setter -------------------

    public void setDriver(MongoDBDriver driver) {
        this.driver = driver;
    }

    public void setCollectionName(String collectionName) {
        this.collectionName = collectionName;
    }

    public void setReconstructMongoId(boolean reconstructMongoId) {
        this.reconstructMongoId = reconstructMongoId;
    }

    //---------------------- getter --------------------

    public DBCollection getCollection() {
        return collection;
    }
}
